package com.qdlc.p2p.dal.model;
import java.util.Date;

import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dao.ActivityAwardLogDao;
import com.qdlc.p2p.dal.dao.ActivityPlanDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.ActivityAwardLog;
import com.qdlc.p2p.dal.dto.ActivityPlan;
import com.qdlc.p2p.dal.dto.Experience;
import com.qdlc.p2p.dal.dto.RateInterest;
import com.qdlc.p2p.dal.dto.UserRedEnvelope;

/**
 * 活动方案业务处理-供外部调用
 * 
 * @author zlhx
 * @version 2.0
 * @date 2015-04-23 14:44
 */
public class ActivityPlanHandleModel {
	
	private ActivityPlan activityPlan;
	
	private ActivityPlanDao activityPlanDao; 
	
	public ActivityPlanHandleModel(){
		
	}
	
	/**
	 * 构造方法 - 主要标识出活动方案的类型
	 * 
	 * @param type
	 */
	public ActivityPlanHandleModel(String type) {
		ActivityPlan ap = new ActivityPlan();
		activityPlanDao = (ActivityPlanDao) BeanUtil.getBean("activityPlanDao");
		ap = activityPlanDao.getActivityPlanByNid(type);
		this.activityPlan = ap;
	}
	
	/**
	 * 活动方案入口
	 * @param param
	 */
	public void entry(ActivityParamModel param)  {
		String type = StringUtil.isNull(param.getType());
			ActivityPlanHandleModel ap = new ActivityPlanHandleModel(type);
			//活动方案校验
			boolean checkResult = check(ap, param);
			
			if(checkResult){
				giveAward(ap.getActivityPlan(),param);
			}
	}
	
	/**
	 * 发放奖励基础发放
	 * @param map
	 * @param activityPlan
	 */
	public void giveAward(ActivityPlan activityPlan, ActivityParamModel param) {
		long userId = param.getUserId();
		double money = param.getMoney();
		Date date = param.getDate();
		//被邀请人Id
		long beInvitedUserId = param.getBeInvitedUserId();
		//分别取出红包规则、体验券规则、加息券规则ID
		long redEnvelopeRuleId = 0;
		long experienceRuleId = 0;
		long uprateRuleId = 0;
		
		UserDao userDao = (UserDao) BeanUtil.getBean("userDao");
		ActivityAwardLogDao activityAwardLogDao = (ActivityAwardLogDao) BeanUtil.getBean("activityAwardLogDao");
		//活动方案发放记录
		ActivityAwardLog activityAwardLog = new ActivityAwardLog();
		activityAwardLog.setMoney(money);
		activityAwardLog.setType(activityPlan.getNid());
		activityAwardLog.setAddTime(date);
		activityAwardLog.setUser(userDao.getUserById(userId));
		activityAwardLog.setBeInvitedUser(userDao.getUserById(beInvitedUserId));
		if(activityPlan.getRedEnvelopeRule()!=null){
			redEnvelopeRuleId = activityPlan.getRedEnvelopeRule().getId();
		}
		if(activityPlan.getExperienceRule()!=null){
			experienceRuleId = activityPlan.getExperienceRule().getId();
		}
		if(activityPlan.getRateRule()!=null){
			uprateRuleId = activityPlan.getRateRule().getId();
		}
		//活动的Nid，用户发放奖励时类型识别
		String nid = activityPlan.getNid();
		//红包
		if(redEnvelopeRuleId>0){
			UserRedEnvelope ur = giveRedEnvelopeAward(userId, money,redEnvelopeRuleId,nid);
			if(ur != null){
				activityAwardLog.setUserRedEnvelope(ur);
			}
		}
		
		//体验券
		if(experienceRuleId>0){
			Experience e = giveExperienceAward(userId, money,experienceRuleId,nid);
			if(e != null){
				activityAwardLog.setExperience(e);
			}
		}

		//加息券
		if(uprateRuleId>0){
			RateInterest r = giveUprateAward(userId, money,uprateRuleId,nid, date);
			if(r != null){
				activityAwardLog.setRateInterest(r);
			}
		}
		//活动奖励记录
		if(redEnvelopeRuleId > 0 || experienceRuleId > 0 || uprateRuleId > 0){
			activityAwardLogDao.save(activityAwardLog);
		}
		
	}
	
	/**
	 * 发放红包奖励 （标准发放方法） 
	 */
	private UserRedEnvelope giveRedEnvelopeAward(long userId,double money, long redEnvelopeRuleId, String nid){
//		UserRedEnvelopeService userRedEnvelopeService = (UserRedEnvelopeService) BeanUtil.getBean("userRedEnvelopeService");
//		try{
//			if(money>0){
//				return userRedEnvelopeService.giveRedEnvelope(userId, money, redEnvelopeRuleId, nid);
//			}else{
//				return userRedEnvelopeService.giveRedEnvelope(userId, redEnvelopeRuleId ,nid);
//			}
//		}catch(Exception e){
//			e.printStackTrace();
//		}
		return null;
	}
	
	/**
	 * 发放体验券 （标准发放方法） 
	 */
	private Experience giveExperienceAward(long userId,double money, long experienceRuleId, String nid){
//		ExperienceExecuter experienceExecuter =  (ExperienceExecuter) ExecuterHelper.doExecuter("experienceExecuter");
//		try {
//			return experienceExecuter.giveExperience(userId, experienceRuleId);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		return null;
	}

	/**
	 * 发放加息券（标准发放方法） 
	 */
	private RateInterest giveUprateAward(long userId,double money, long uprateRuleId, String nid, Date date){
//		RateInterestService rateInterestService = (RateInterestService) BeanUtil.getBean("rateInterestService");
//		try{
//			return rateInterestService.extendRateTicket(uprateRuleId, userId, money, date);
//		}catch(Exception e){
//			e.printStackTrace();
//		}
		return null;
	}
	
	/**
	 * 规则校验
	 * 
	 * @return
	 */
	private boolean check(ActivityPlanHandleModel apModel,ActivityParamModel param) {
		if(apModel==null){
			return false;
		}
		
		long userId = param.getUserId();
		if(userId<=0){
			return false;
		}
		
		ActivityPlan activityPlan = apModel.getActivityPlan();
		if (activityPlan == null) {
			return false;
		}
		
		Date start_time = activityPlan.getStartTime();
		Date end_time = activityPlan.getEndTime();
		int status = activityPlan.getStatus();
		// 活动未激活状态
		if (status != 1) {
			return false;
		}
		
		Date date = param.getDate();
		if(date == null){
			date = DateUtil.getNow();
		}
		//开始结束时间判定
		if (start_time.getTime() > date.getTime()
				|| end_time.getTime() < date.getTime()) {
			return false;
		}
		return true;
	}

	

	public ActivityPlan getActivityPlan() {
		return activityPlan;
	}

	public void setActivityPlan(ActivityPlan activityPlan) {
		this.activityPlan = activityPlan;
	}

}
